package www.reagnetbox.com.view;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import www.reagnetbox.com.R;
import www.reagnetbox.com.callback.SimpleCallback;

public class AuthPopup extends CenterPopupView {
    Context context;
    String title;
    String subTitle;
    SimpleCallback callback;
    private TextView tvInput;

    public AuthPopup(Context context, String title, String subTitle, SimpleCallback callback) {
        super(context);
        this.context=context;
        this.title = title;
        this.subTitle = subTitle;
        this.callback = callback;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_auth;
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenWidth(getContext()) * .85f);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ((TextView) findViewById(R.id.tv_title)).setText(title);
        ((TextView) findViewById(R.id.tv_sub_title)).setText(subTitle==null?"请刷IC卡或输入鉴权码:":subTitle);

        tvInput = findViewById(R.id.tv_input);

        findViewById(R.id.tv_1).setOnClickListener(view -> insert("1"));
        findViewById(R.id.tv_2).setOnClickListener(view -> insert("2"));
        findViewById(R.id.tv_3).setOnClickListener(view -> insert("3"));
        findViewById(R.id.tv_4).setOnClickListener(view -> insert("4"));
        findViewById(R.id.tv_5).setOnClickListener(view -> insert("5"));
        findViewById(R.id.tv_6).setOnClickListener(view -> insert("6"));
        findViewById(R.id.tv_7).setOnClickListener(view -> insert("7"));
        findViewById(R.id.tv_8).setOnClickListener(view -> insert("8"));
        findViewById(R.id.tv_9).setOnClickListener(view -> insert("9"));
        findViewById(R.id.tv_0).setOnClickListener(view -> insert("0"));

        findViewById(R.id.tv_clear).setOnClickListener(view -> tvInput.setText(""));
        findViewById(R.id.tv_del).setOnClickListener(view -> {
            String input = tvInput.getText().toString();
            if (!input.equals("")){
                tvInput.setText(input.substring(0,input.length()-1));
            }
        });

        findViewById(R.id.tv_cancel).setOnClickListener(v -> dismiss());
        findViewById(R.id.tv_ok).setOnClickListener(v -> {
            dismiss();
            callback.fun();
        });
    }

    private void insert(String input) {
        tvInput.setText(tvInput.getText().toString()+input);
    }
}
